#!/usr/bin/env bash

# @author Yakir.King-wangyingkai
# @date 2021/2/13 18:38
# @Email:wangyingkai3@jd.com

DISK_DEV=$1
MOUNT_DIR=$2

function print_usage(){
 echo "Usage -- 请输入两个参数：sh diskMount.sh DISK_DEV MOUNT_DIR"
 exit 2
}

if [ $# -ne 2 ]
then
 print_usage
fi

echo "创建挂载盘"
if [ -d ${MOUNT_DIR} ]
then
 read -p "磁盘已存在，请确认是否仍需挂载！[Yes/No]" res
 case ${res} in
  Yes | yes | Y | y)
   echo "仍需挂载至${MOUNT_DIR}."
   ;;
  *)
   echo "no"
   exit 1
   ;;
 esac
fi

echo "分区规则，将此盘格式化为1块 xfs的盘"
parted ${DISK_DEV} "mklabel gpt"
parted ${DISK_DEV} "mkpart primary xfs 0% 100%"

echo "建立文件系统：mkfs，格式为xfs"
mkfs -t xfs ${DISK_DEV}1
mkdir ${MOUNT_DIR}

echo "挂载..."
mount ${DISK_DEV}1 ${MOUNT_DIR}

echo "检验..."
df -h

echo "---------------------更新/etc/fstab------------------------------"
BLK_UUID=`blkid ${DISK_DEV}1 | awk -F "\"" '{print $2}'`
echo "UUID=${BLK_UUID} ${MOUNT_DIR} xfs defaults 1 2" >> /etc/fstab
cat /etc/fstab
